﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblLoan2PhoneDAO
    {
        DBService _db = null;
        public TblLoan2PhoneDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public DataTable LoadIDLPhone()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select LPhoneID, LoanStatus from TblLoan2Phone where LoanStatus = 0 ", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public DataTable GetAll()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select LPhoneID, LPhoneBrand, LPhoneModel, LPIMENo, AccessoriresIncluded, Deposit, LoanFee," +
                                             "case LoanStatus when 0 then 'Not Loan' else 'Loaned' end as LoanStatus, Notes from TblLoan2Phone", 
                                         false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public DataTable GetLPhoneByID(string LPhoneID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblLoan2Phone where LPhoneID = @LPhoneID ",
                    new string[] { "@LPhoneID" },
                    new object[] { LPhoneID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool InsUpd_L2Phone(TblLoan2Phone L2Phone, int mode)
        {
            bool flag = false;
            try 
            {
                _db.ExeSP_NonQuery("sp_TblLoan2Phone_InsUpd",
                    new string[] {"@LPhoneID", "@LPhoneBrand", "@LPhoneModel", "@LPIMENo", "@AccessoriresIncluded",
							      "@Deposit", "@LoanFee", "@LoanStatus", "@Notes", "@mode" },
                    new object[] {L2Phone.LPhoneID, L2Phone.LPhoneBrand, L2Phone.LPhoneModel, L2Phone.LPIMENo, L2Phone.AccessoriresIncluded,
                                 L2Phone.Deposit, L2Phone.LoanFee, L2Phone.LoanStatus, L2Phone.Notes, mode},
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        public bool DeleteLPhone(string LPhoneID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblLoan2Phone where LPhoneID = @LPhoneID",
                    new string[] { "@LPhoneID" },
                    new object[] { LPhoneID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable Update(int LoanStatus, string LPhoneID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("Update TblLoan2Phone set LoanStatus = @LoanStatus where LPhoneID = @LPhoneID",
                    new string[] { "@LoanStatus", "@LPhoneID" },
                    new object[] { LoanStatus, LPhoneID }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
